package com.sllp.C71S3TsllpShop.listener;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import com.sllp.C71S3TsllpShop.bean.BigType;
import com.sllp.C71S3TsllpShop.bean.BigTypeExample;
import com.sllp.C71S3TsllpShop.bean.User;
import com.sllp.C71S3TsllpShop.biz.impl.BigTypeBizImpl;
import com.sllp.C71S3TsllpShop.biz.impl.UserBizImpl;
import com.sllp.C71S3TsllpShop.dao.BigTypeMapper;

@WebListener//监听器注解
public class OpOnlineUserSessionListener implements HttpSessionListener {
 
	@Resource
	private BigTypeMapper btm;
	@Resource
	private BigTypeBizImpl btbiz;
	@Resource
	private UserBizImpl ub;
	@Resource
	private HttpServletRequest request;
	@Resource
	private HttpServletResponse response;
	
	/**
	 * session被销毁时触发,如下情况
	 * 		1.主动调用session.invalidate()
	 * 		2.session超时
	 */
	@Override
	public void sessionDestroyed(HttpSessionEvent event) {
		HttpSession session = event.getSession();
		//获取当前用户信息
		User loginUser = (User)session.getAttribute("loginUser");
		if(loginUser != null) {
			loginUser.setLoginState(2);
			ub.userLogout(loginUser);
		}
	}
	
	//session被创建时触发
	@Override
	public void sessionCreated(HttpSessionEvent event) {
		HttpSession session = event.getSession();
		//全部商品分类显示
		BigTypeExample bigTypeExample = new BigTypeExample();
		bigTypeExample.createCriteria().andStateEqualTo(2);
		List<BigType> source=btm.selectByExample(bigTypeExample);
		List<List<BigType>> lists=btbiz.findAllBigType(source);
		session.setAttribute("btlist", lists); 
	}
 
}
